<?php
/**
 *      帮助中心
 *      [Haidao] (C)2013-2099 Dmibox Science and technology co., LTD.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      http://www.haidao.la
 *      tel:400-600-2042
 */
class UnionController extends HomeBaseController{
    public function _initialize() {
       parent::_initialize();
	   
       $this->db = model('Union');
    }
    /* 帮助中心首页 */
    public function index(){
		$page=PAGE;
		//p($page);
		$union_type = m('union_type')->order("id asc")->select();
		$union_new = m('union')->order("id desc")->limit(4)->select();
		$region = model('region')->where(array('parent_id' => 1))->select();
        $city = model('region')->where(array('parent_id' => $_GET['province']))->select();
        $district = model('region')->where(array('parent_id' => $_GET['city']))->select();
		$region_list = model('region')->select();
		foreach($region_list as $val){
			$city_info[$val["area_id"]]	=$val["area_name"];
		}

		if($_GET["fenlei"]!="" and $_GET["fenlei"]!=-1){
			$sqlmap["u.fenlei"]=$_GET["fenlei"];	
		}
		if($_GET["province"]!="" and $_GET["province"]!=-1){
			$sqlmap["u.province"]=$_GET["province"];	
		}
		if($_GET["city"]!="" and $_GET["city"]!=-1){
			$sqlmap["u.city"]=$_GET["city"];	
		}
		if($_GET["district"]!="" and $_GET["district"]!=-1){
			$sqlmap["u.district"]=$_GET["district"];	
		}
		if($_GET["title"]!=""){
			$sqlmap["u.company_name"]=array('like','%'.$_GET["title"].'%');	
		}
		//if ($where) $sqlmap['_string'] = $where;
		$count =  m('Union u')->join("LEFT JOIN __UNION_TYPE__ t on t.id=u.fenlei")->field("u.*,t.type_name")->where($sqlmap)->count();
		libfile('Page');
		$Page = new Page($count,12);
        //$Page->setConfig('prev','');
        //$Page->setConfig('next','');
        //$Page->setConfig('theme','%upPage% %linkPage% %downPage%');
        $Page->listRows = 12;
		$list_list=$list['list'] =  m('Union u')->join("LEFT JOIN __UNION_TYPE__ t on t.id=u.fenlei")->field("u.*,t.type_name")->where($sqlmap)->page(PAGE,12)->order('u.id DESC')->select();
		$list['page'] = $Page->show();
        $list['c'] =$count;
		$SEO=seo(0,"联盟商家");
        include template('union_index');
    }
	/* 帮助中心首页 */
    public function lists(){
		$page=PAGE;
		//p($page);
		$union_type = m('union_type')->order("id asc")->select();
		$union_new = m('union')->order("id desc")->limit(4)->select();
		$region = model('region')->where(array('parent_id' => 1))->select();
        $city = model('region')->where(array('parent_id' => $_GET['province']))->select();
        $district = model('region')->where(array('parent_id' => $_GET['city']))->select();
		$region_list = model('region')->select();
		foreach($region_list as $val){
			$city_info[$val["area_id"]]	=$val["area_name"];
		}

		if($_GET["fenlei"]!="" and $_GET["fenlei"]!=-1){
			$sqlmap["u.fenlei"]=$_GET["fenlei"];	
		}
		if($_GET["province"]!="" and $_GET["province"]!=-1){
			$sqlmap["u.province"]=$_GET["province"];	
		}
		if($_GET["city"]!="" and $_GET["city"]!=-1){
			$sqlmap["u.city"]=$_GET["city"];	
		}
		if($_GET["district"]!="" and $_GET["district"]!=-1){
			$sqlmap["u.district"]=$_GET["district"];	
		}
		if($_GET["title"]!=""){
			$sqlmap["u.company_name"]=array('like','%'.$_GET["title"].'%');	
		}
		//if ($where) $sqlmap['_string'] = $where;
		$count =  m('Union u')->join("LEFT JOIN __UNION_TYPE__ t on t.id=u.fenlei")->field("u.*,t.type_name")->where($sqlmap)->count();
		libfile('Page');
		$Page = new Page($count,12);
        //$Page->setConfig('prev','');
        //$Page->setConfig('next','');
        //$Page->setConfig('theme','%upPage% %linkPage% %downPage%');
        $Page->listRows = 12;
		$list['list'] =  m('Union u')->join("LEFT JOIN __UNION_TYPE__ t on t.id=u.fenlei")->field("u.*,t.type_name")->where($sqlmap)->page(PAGE,12)->order('u.id DESC')->select();
		$list['page'] = $Page->show();
        $list['c'] =$count;
		$SEO=seo(0,"联盟商家");
        include template('union_lists');
    }
	 public function detail(){
	 	$region = model('region')->where(array('parent_id' => 1))->select();
        $city = model('region')->where(array('parent_id' => $_GET['province']))->select();
        $district = model('region')->where(array('parent_id' => $_GET['city']))->select();
		$region_list = model('region')->select();
		foreach($region_list as $val){
			$city_info[$val["area_id"]]	=$val["area_name"];
		}
		$page=PAGE;
		/* 获取顶级栏目 */
		$id=I("id");
		$field = "a.*,b.area_name as prov_name,c.area_name as city_name,d.area_name as dist_name,f.type_name as hangye";
		$join = " LEFT JOIN `__REGION__` as b ON a.province=b.area_id";
		$join.= " LEFT JOIN `__REGION__` as c ON a.city=c.area_id";
		$join.= " LEFT JOIN `__REGION__` as d ON a.district=d.area_id";	
		$join.= " LEFT JOIN `__UNION_TYPE__` as f ON a.fenlei=f.id";	
		$info=M("union a")->field($field)->join($join)->where("a.id=".$id)->find();
		$address=file_get_contents("http://api.map.baidu.com/geocoder/v2/?ak=XocxxVGg5XzUFPd1rUWGTICw&output=json&address=".$info["prov_name"].$info["city_name"].$info["dist_name"].$info["address"]."&city=".$info["prov_name"].$info["city_name"]);
		$map=object_array(json_decode($address));
		$SEO=seo(0,$info["company_name"]."-联盟商家");
        include template('union_detail');
    }
	/**
     * ajax获取地区
     *@author wj 
     *@date  2014-10-14
     */
    public function getArea() {
        $where['parent_id'] = $_GET['areaId'];
        $area = M('region')->where($where)->select();
        $this->ajaxReturn($area);
    }
}